package com.mall.aop;


import com.mall.uitl.JsonUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;

@Component
@Aspect
public class ExceptionInterceptor {


    Logger logger = LoggerFactory.getLogger(this.getClass());//ExceptionInterceptor

    @Pointcut("execution(public * com.mall.action.*.*(..))")
    public void pointCut() {
    }

    @Around(value = "pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) {
        String className = joinPoint.getTarget().getClass().getName();  //joinPoint.getTarget().getClass()
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        logger.debug("类名:" + className + "方法名:" + methodName + "方法的参数:" + Arrays.toString(args));

        try {
            Object retVal = joinPoint.proceed();//调用empAcation-->addEmp方法  retVal就是addEmp方法返回值
            return retVal;
        } catch (Throwable throwable) {
            // throwable.printStackTrace();
            // logger.
            logger.error("类名:" + className + "方法名:" + methodName + "方法的参数:" + Arrays.toString(args), throwable);
            throwable.printStackTrace();
            JsonUtil jsonUtil = new JsonUtil();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:" + throwable.getMessage());
            return jsonUtil;
        }

    }
}
